# 库1
import time
import random

# 库2
import database.player as player
import database.monster as monster


# 面板显示
def panel(role):
    print('\n')
    print(role['name'], end=' ')
    print('Lv', role['lv'])
    print('HP:', role['HP'])
    print('MP:', role['MP'])
    print('攻击:', '%d (' % (role['ATK'] + role['weapon']['ATK']), role['ATK'], ',', role['weapon']['ATK'], ')')
    print('防御:', '%d (' % (role['DEF'] + role['armor']['DEF']), role['DEF'], ',', role['armor']['DEF'], ')')
    print('暴击:', '%d%% (' % ((role['crper'] + role['lucky'] + role['ornaments']['lucky']) * 100), role['crper'], ',',
          role['lucky'], ',', role['ornaments']['lucky'], ')')
    print('幸运:', '%d%% (' % ((role['lucky'] + role['ornaments']['lucky']) * 100), role['lucky'], ',',
          role['ornaments']['lucky'], ')\n')
    time.sleep(1)


# 面板计算
def panel_calculation(role0, role):
    role0['name'] = role['name']
    role0['lv'] = role['lv']
    role0['mstlv'] = role['mstlv']
    role0['HP'] = role['HP']
    role0['MP'] = role['MP']
    role0['ATK'] = role['ATK'] + role['weapon']['ATK']
    role0['DEF'] = role['DEF'] + role['armor']['DEF']
    role0['crper'] = role['crper'] + role['lucky'] + role['ornaments']['lucky']
    role0['lucky'] = role['lucky'] + role['ornaments']['lucky']


# 是否选择判定
def yon():
    i = 0
    while i == 0:
        i = input('您的选择：Y(Yes) or N(No):')
        if i == 'Y' or i == 'y':
            return True
        elif i == 'N' or i == 'n':
            return False
        else:
            print('请确认你的选择，Y or N ')
            i = 0


# 暴击判定
def deter(crper):
    contrast = random.random()
    if crper >= contrast:
        return True
    else:
        return False


# roll点判定
def roll():
    i = random.randint(1, 12)
    print('你获得了 %d 点数' % i)
    return i


# 大成功判定
def wow(lucky):
    contrast = random.random()
    if lucky >= contrast:
        return True
    else:
        return False


# 战斗判定
def fight(mst, man):
    # 数值初始化
    panel_calculation(monster.monster0, mst)
    panel_calculation(player.player0, man)
    # 战斗结算
    t = True
    while t:
        # player攻击
        if player.player0['HP'] > 0:
            print('你攻击了', monster.monster0['name'], end=' ')
            # 暴击判定
            if deter(player.player0['crper']):
                player.player0['ATK'] = player.player0['ATK'] * 2
                atk = player.player0['ATK'] - monster.monster0['DEF']
                # 无效判定
                if atk > 0:
                    monster.monster0['HP'] -= atk
                else:
                    monster.monster0['HP'] -= 1
                # 攻击恢复
                player.player0['ATK'] = player.player0['ATK'] / 2
                print('获得了暴击', end=' ')
            else:
                atk = player.player0['ATK'] - monster.monster0['DEF']
                # 无效判定
                if atk > 0:
                    monster.monster0['HP'] -= atk
                else:
                    monster.monster0['HP'] -= 1
                print('未获得暴击', end=' ')
            print('怪物血量：', monster.monster0['HP'])
        # monster攻击
        if monster.monster0['HP'] > 0:
            print(monster.monster0['name'], '攻击了你', end=' ')
            # 暴击判定
            if deter(monster.monster0['crper']):
                monster.monster0['ATK'] = monster.monster0['ATK'] * 2
                atk = monster.monster0['ATK'] - player.player0['DEF']
                # 无效判定
                if atk > 0:
                    player.player0['HP'] -= atk
                else:
                    player.player0['HP'] -= 1
                # 攻击恢复
                monster.monster0['ATK'] = monster.monster0['ATK'] / 2
                print('获得了暴击', end=' ')
            else:
                atk = monster.monster0['ATK'] - player.player0['DEF']
                # 无效判定
                if atk > 0:
                    player.player0['HP'] -= atk
                else:
                    player.player0['HP'] -= 1
                print('未获得暴击', end=' ')
            print('玩家血量：', player.player0['HP'], '\n')
        time.sleep(0.5)
        # 结果判定
        if player.player0['HP'] <= 0:
            print('\n')
            print('玩家死亡')
            t = False
        if monster.monster0['HP'] <= 0:
            print('\n')
            print('怪物死亡')
            t = False

# 怪物刷新
